package com.sxm.yiwei.adapters;

import android.content.Context;
import android.content.DialogInterface;

import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.sxm.yiwei.R;
import com.sxm.yiwei.activitis.BuyerServiceListActivity;
import com.sxm.yiwei.beans.ServiceList;
import com.sxm.yiwei.tools.CastTime;
import com.sxm.yiwei.utils.InternetAddressUtis;

import org.xutils.x;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 *
 * Created by KuKa on 2017/7/12.
 * 购买服务列表
 */

public class BuyerServiceListAdapter extends BaseAdapter {
    OnClickItemButtonListener o;
    Context context;
   public List<ServiceList.DataBean> mlist;
     int TYPE = 1;
    public BuyerServiceListAdapter(Context context, List<ServiceList.DataBean> mlist ,int TYPE) {
        this.context=context;
        this.mlist=mlist;
        this.TYPE = TYPE;
    }

    @Override
    public int getCount() {
        return mlist.size()==0?0:mlist.size();
    }

    @Override
    public Object getItem(int position) {
        return mlist.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent) {
        View view;
        ViewHolder holder;
        if (convertView==null){
            view = View.inflate(context, R.layout.buyer_listitem,null);
            holder = new ViewHolder();
            holder.img = (ImageView) view.findViewById(R.id.buyer_serviceimg);
            holder.explain = (TextView) view.findViewById(R.id.buyer_serviceexplain);
            holder.expalaintext = (TextView) view.findViewById(R.id.buyer_serviceexplaintext);
            holder.pricetext = (TextView) view.findViewById(R.id.buyer_servicepricetext);
            holder.bookno = (TextView) view.findViewById(R.id.buyer_serviceaddprice);
            holder.billprice = (TextView) view.findViewById(R.id.buyer_billprice);
            holder.num = (TextView) view.findViewById(R.id.buyer_service_num);
            holder.time = (TextView) view.findViewById(R.id.buyer_servicetime);
            holder.bookct = (TextView) view.findViewById(R.id.buyer_servicebooknotext);
            holder.addprice = (TextView) view.findViewById(R.id.buyer_serviceaddprice);
            holder.addpricebt = (TextView) view.findViewById(R.id.buyer_addpricebt);
            holder.cancelbt = (TextView) view.findViewById(R.id.buyer_item_cancelbt);
            holder.cellbt = (LinearLayout) view.findViewById(R.id.buyer_item_cellbt);
            holder.weiyiabt = (LinearLayout) view.findViewById(R.id.buyer_item_weiyiabt);
            holder.finishbt = (LinearLayout) view.findViewById(R.id.buyer_finishbt);
            holder.view1 = (LinearLayout) view.findViewById(R.id.buyer_listiem_view);
            holder.addmoney = (TextView) view.findViewById(R.id.buyer_listaddmoney);
            holder.buyername = (TextView) view.findViewById(R.id.buyer_name);
            holder.buyeraddress = (TextView) view.findViewById(R.id.buyer_address);
            holder.vendorname = (TextView) view.findViewById(R.id.vendor_name);
            holder.vendoraddress = (TextView) view.findViewById(R.id.vendor_address);
            holder.itemview = (RelativeLayout) view.findViewById(R.id.buyer_item_view);
            view.setTag(holder);
        }else {
            view=convertView;
            holder= (ViewHolder) view.getTag();
        }

        if (TYPE==1&TYPE==5&TYPE==6){


        }else {
            int isMakeup = mlist.get(position).getIsMakeup();
            if (isMakeup==1){
                holder.view1.setAlpha(0.2f);
                holder.addmoney.setText(mlist.get(position).getMakeupAmount()+" 元");
                holder.view1.setVisibility(View.VISIBLE);
            }else {

                holder.view1.setVisibility(View.GONE);

            }
        }
        if (TYPE==1){
            holder.cancelbt.setText("删除");
            holder.addpricebt.setText("立即支付");
        }else if (TYPE==2){
            holder.cancelbt.setText("取消");
            holder.addpricebt.setText("加价");
        }else if (TYPE==3){
            holder.cancelbt.setText("申诉");
            holder.addpricebt.setText("加价");
            holder.finishbt.setVisibility(View.GONE);
        }else if (TYPE==4){
            holder.cancelbt.setText("申诉");
            holder.addpricebt.setText("加价");
            holder.finishbt.setVisibility(View.VISIBLE);
        }else if (TYPE==5){
            Long endTime = mlist.get(position).getEndTime();
            Long commentTime = mlist.get(position).getCommentTime();
            if (commentTime!=null){
                holder.addpricebt.setVisibility(View.GONE);
            }else {
                holder.addpricebt.setText("评价");
            }
            if (endTime!=null){
                boolean b = CastTime.endTime(endTime);
                if (b){
                    holder.cancelbt.setVisibility(View.GONE);
                }else {
                    holder.cancelbt.setText("申诉");
                }
            }else {}

            holder.finishbt.setVisibility(View.GONE);


        }else {
            holder.cancelbt.setText("查看");
            holder.finishbt.setVisibility(View.GONE);
            holder.addpricebt.setVisibility(View.GONE);
        }
        holder.buyername.setText("买家:"+mlist.get(position).getContactName()+" "+mlist.get(position).getContactPhone());
        holder.buyeraddress.setText(mlist.get(position).getContactAddress()+mlist.get(position).getContactHouseNumber());
        holder.vendorname.setText("卖家:"+mlist.get(position).getUserMobile());
        holder.vendoraddress.setText(mlist.get(position).getShopAddress());
        holder.explain.setText(mlist.get(position).getName());
        holder.num.setText("数量: "+String.valueOf(mlist.get(position).getCount()));
        holder.pricetext.setText(String.valueOf(mlist.get(position).getTotalAmount())+" 元");
        holder.bookct.setText(mlist.get(position).getOrderCode());
        holder.time.setText(CastTime.CastTime(mlist.get(position).getBuyTime()));
        holder.billprice.setText("单价 "+mlist.get(position).getAmount()+" 元/单");
        if (mlist.get(position).getMakeupCount()==0){
            holder.addprice.setText("加价 0 次");
        }else {
            holder.addprice.setText("加价 "+mlist.get(position).getMakeupCount()+" 次");
        }
        if (mlist.get(position).getMessage().equals("")&mlist.get(position).getMessage().equals(null)){
            holder.expalaintext.setText("");
        }else {
            holder.expalaintext.setText(""+mlist.get(position).getMessage());
        }

        x.image().bind(holder.img, InternetAddressUtis.IMG_URL+mlist.get(position).getAvatar());

        holder.addpricebt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener(position);
                }
            }
        });
        holder.weiyiabt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener1(position);
                }
            }
        });
        holder.cellbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener2(position);
                }
            }
        });
        holder.cancelbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener3(position);
                }
            }
        });
        holder.finishbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener4(position);
                }
            }
        });
        holder.view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener5(position);
                }
            }
        });
        holder.itemview.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (o!=null){
                    o.OnClickItemButtonListener6(position);
                }
            }
        });
        notifyDataSetChanged();
        return view;
    }

    public class ViewHolder{
        ImageView img;

       public TextView buyername,buyeraddress,vendorname,vendoraddress,addmoney,billprice,expalaintext,explain,pricetext,num,addprice,bookno,addpricebt,cancelbt,time,bookct;
        LinearLayout view1,cellbt,weiyiabt,finishbt;
        RelativeLayout itemview;
    }
    public interface OnClickItemButtonListener{
        void OnClickItemButtonListener(int position);
        void OnClickItemButtonListener1(int position);
        void OnClickItemButtonListener2(int position);
        void OnClickItemButtonListener3(int position);
        void OnClickItemButtonListener4(int position);
        void OnClickItemButtonListener5(int position);
        void OnClickItemButtonListener6(int position);
    }
    public void setOnClickItemButtonListener(OnClickItemButtonListener o){
        this.o=o;
    }
}
